package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    TeacherService tService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteStudentById();
                    break;
                case "3":
                    updateTeacherById();
                    break;
                case "4":
                    showStudent();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    public void updateTeacherById() {
        String id;
        while (true) {
            System.out.println("请输入要修改的老师id");
            id = sc.next();
            boolean flag = tService.isExists(id);
            if (flag) {
                break;
            } else {
                System.out.println("您输入的id不存在");
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher t = new Teacher(id, name, age, birthday);
        tService.updateTeacherById(t);
        System.out.println("修改成功");
    }


    public void deleteStudentById() {
        System.out.println("请输入要删除的id");
        String id = sc.next();
        boolean flag = tService.isExists(id);
        if (flag) {
            tService.deleteTeacherById(id);
            System.out.println("删除成功");
        } else {
            System.out.println("无此id");
        }
    }

    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师的id");
            id = sc.next();
            boolean flag = tService.isExists(id);
            if (flag) {
                System.out.println("您输入的id重复,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入老师的姓名");
        String name = sc.next();
        System.out.println("请输入老师的年龄");
        String age = sc.next();
        System.out.println("请输入老师的生日");
        String brithday = sc.next();
        Teacher teacher = new Teacher(id, name, age, brithday);
        tService.addTeacher(teacher);
        System.out.println("添加成功");
    }

    public void showStudent() {
        Teacher[] ts=tService.showTeacher();
        if (ts==null){
            System.out.println("暂无数据,请先添加");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < ts.length; i++) {
            Teacher t = ts[i];
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t" + t.getBrithday());
            }
        }
    }
}
